package cn.kgc.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author: mosin
 * @version: v1.0  2022/7/8
 */
@Controller
@RequestMapping("file")
public class FileController {

    @RequestMapping("upload")
    public  String upload(MultipartFile file){

        //获取文件的名字
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //获取文件的扩展名
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        //拼接uuid文件名
        String finalFileName = uuid+"."+filenameExtension;


        //文件上传
        try {
            file.transferTo(new File("D:\\upload", finalFileName));
            System.out.println("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件上传失败");
        }
        return "index";
    }

}
